package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HoDormitoryInfo;
import com.ruoyi.system.service.IHoDormitoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HoOccupancyInfoMapper;
import com.ruoyi.system.domain.HoOccupancyInfo;
import com.ruoyi.system.service.IHoOccupancyInfoService;
import org.springframework.util.CollectionUtils;

/**
 * 入住信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-10
 */
@Service
public class HoOccupancyInfoServiceImpl implements IHoOccupancyInfoService {
    @Autowired
    private HoOccupancyInfoMapper hoOccupancyInfoMapper;

    @Autowired
    private IHoDormitoryInfoService hoDormitoryInfoService;

    /**
     * 查询入住信息
     *
     * @param id 入住信息主键
     * @return 入住信息
     */
    @Override
    public HoOccupancyInfo selectHoOccupancyInfoById(Long id) {
        return hoOccupancyInfoMapper.selectHoOccupancyInfoById(id);
    }

    /**
     * 查询入住信息列表
     *
     * @param hoOccupancyInfo 入住信息
     * @return 入住信息
     */
    @Override
    public List<HoOccupancyInfo> selectHoOccupancyInfoList(HoOccupancyInfo hoOccupancyInfo) {
        List<HoOccupancyInfo> list = hoOccupancyInfoMapper.selectHoOccupancyInfoList(hoOccupancyInfo);
        if (!CollectionUtils.isEmpty(list)) {
            for (HoOccupancyInfo occupancyInfo : list) {
                if (null != occupancyInfo.getDormitoryId()) {
                    HoDormitoryInfo hoDormitoryInfo = new HoDormitoryInfo();
                    hoDormitoryInfo.setId(occupancyInfo.getDormitoryId());
                    List<HoDormitoryInfo> hoDormitoryInfos = hoDormitoryInfoService.queryHoDormitoryInfoList(hoDormitoryInfo);
                    if (!CollectionUtils.isEmpty(hoDormitoryInfos)) {
                        occupancyInfo.setLabel(hoDormitoryInfos.get(0).getLabel());
                    }
                }
            }
        }
        return list;
    }

    /**
     * 新增入住信息
     *
     * @param hoOccupancyInfo 入住信息
     * @return 结果
     */
    @Override
    public int insertHoOccupancyInfo(HoOccupancyInfo hoOccupancyInfo) {
        hoOccupancyInfo.setCreateTime(DateUtils.getNowDate());
        return hoOccupancyInfoMapper.insertHoOccupancyInfo(hoOccupancyInfo);
    }

    /**
     * 修改入住信息
     *
     * @param hoOccupancyInfo 入住信息
     * @return 结果
     */
    @Override
    public int updateHoOccupancyInfo(HoOccupancyInfo hoOccupancyInfo) {
        hoOccupancyInfo.setUpdateTime(DateUtils.getNowDate());
        return hoOccupancyInfoMapper.updateHoOccupancyInfo(hoOccupancyInfo);
    }

    /**
     * 批量删除入住信息
     *
     * @param ids 需要删除的入住信息主键
     * @return 结果
     */
    @Override
    public int deleteHoOccupancyInfoByIds(Long[] ids) {
        return hoOccupancyInfoMapper.deleteHoOccupancyInfoByIds(ids);
    }

    /**
     * 删除入住信息信息
     *
     * @param id 入住信息主键
     * @return 结果
     */
    @Override
    public int deleteHoOccupancyInfoById(Long id) {
        return hoOccupancyInfoMapper.deleteHoOccupancyInfoById(id);
    }

    @Override
    public void changeApplication(HoOccupancyInfo hoOccupancyInfo, Long dormitoryId, Long userId, String applicationInfo, Date checkOutTime) {
        Date nowDate = DateUtils.getNowDate();
        // 按照新宿舍办理入住
        HoOccupancyInfo newOccupancyInfo = new HoOccupancyInfo();
        newOccupancyInfo.setStudentId(hoOccupancyInfo.getStudentId());
        newOccupancyInfo.setName(hoOccupancyInfo.getName());
        newOccupancyInfo.setStudentNum(hoOccupancyInfo.getStudentNum());
        newOccupancyInfo.setDormitoryId(dormitoryId);
        newOccupancyInfo.setCheckInOfficer(userId);
        newOccupancyInfo.setCheckInTime(checkOutTime);
        newOccupancyInfo.setStatus("1");
        newOccupancyInfo.setCreateUserId(userId);
        newOccupancyInfo.setCreateTime(nowDate);
        hoOccupancyInfoMapper.insertHoOccupancyInfo(newOccupancyInfo);
        // 原本的办理退宿
        hoOccupancyInfo.setCheckOutTime(checkOutTime);
        hoOccupancyInfo.setCheckOutOfficer(userId);
        hoOccupancyInfo.setCheckOutType("2");
        hoOccupancyInfo.setApplicationInfo(applicationInfo);
        hoOccupancyInfo.setStatus("0");
        hoOccupancyInfo.setUpdateUserId(userId);
        hoOccupancyInfo.setUpdateTime(nowDate);
        hoOccupancyInfoMapper.updateHoOccupancyInfo(hoOccupancyInfo);
    }

    @Override
    public void checkoutApplication(HoOccupancyInfo hoOccupancyInfo, Long userId, String applicationInfo, Date checkOutTime) {
        Date nowDate = DateUtils.getNowDate();
        // 原本的办理退宿
        hoOccupancyInfo.setCheckOutTime(checkOutTime);
        hoOccupancyInfo.setCheckOutOfficer(userId);
        hoOccupancyInfo.setCheckOutType("1");
        hoOccupancyInfo.setApplicationInfo(applicationInfo);
        hoOccupancyInfo.setStatus("0");
        hoOccupancyInfo.setUpdateUserId(userId);
        hoOccupancyInfo.setUpdateTime(nowDate);
        hoOccupancyInfoMapper.updateHoOccupancyInfo(hoOccupancyInfo);
    }
}
